package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.service.DetailsService;
import com.zr.service.impl.DetailsServiceImpl;

import net.sf.json.JSONObject;

public class DetailsController extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String details = "/details.do";
		String detail = "/detail.do";
		if (details.equals(req.getServletPath())) {
			req.setAttribute("cid", req.getParameter("cid"));
			req.getRequestDispatcher("details.jsp").forward(req, resp);
			
		}
		
		if (detail.equals(req.getServletPath())){
			int cid = Integer.parseInt(req.getParameter("cid"));
			DetailsService ds = new DetailsServiceImpl();
			JSONObject jObject = ds.getContentById(cid);
			
			PrintWriter pWriter = resp.getWriter();
			resp.setContentType("text/html; charset=utf-8");
			pWriter.write(jObject.toString());
			jObject.clear();
			pWriter.flush();
			pWriter.close();
			
		}
		
	}
	
}
